package com.meta.pic.util

import androidx.recyclerview.widget.GridLayoutManager
import com.airbnb.epoxy.EpoxyControllerAdapter
import com.airbnb.epoxy.EpoxyModel
import com.airbnb.epoxy.EpoxyRecyclerView



inline fun EpoxyRecyclerView.createGridLookup(crossinline map: (model: EpoxyModel<*>) -> Int): GridLayoutManager.SpanSizeLookup {
    val myAdapter = (adapter as? EpoxyControllerAdapter) ?: throw IllegalAccessException("adapter is not EpoxyControllerAdapter")
    val manager = (layoutManager as? GridLayoutManager) ?: throw IllegalAccessException("layoutManager is not GridLayoutManager")
    return object : GridLayoutManager.SpanSizeLookup() {
        override fun getSpanSize(position: Int): Int {
            val models = myAdapter.copyOfModels
            if (models.isEmpty()) return manager.spanCount
            val model = models[position]
            return map(model)
        }
    }
}